Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ProjectTracker

    Public MustInherit Class ExitCriteriaEdit
        Inherits Entities.Modules.PortalModuleBase

#Region "Controls"
        Protected WithEvents fldCriteria As System.Web.UI.WebControls.TextBox
        Protected WithEvents fldDescription As System.Web.UI.WebControls.TextBox
        Protected WithEvents rfvCriteria As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents rfvDescription As System.Web.UI.WebControls.RequiredFieldValidator

        Protected WithEvents cmdUpdate As System.Web.UI.WebControls.LinkButton
        Protected WithEvents cmdCancel As System.Web.UI.WebControls.LinkButton
        Protected WithEvents cmdDelete As System.Web.UI.WebControls.LinkButton

#End Region

#Region "Private Members"
        Private itemId As Integer
        Private stateId As Integer
#End Region

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim objController As New ExitCriteriaController
                Dim objInfo As New ExitCriteriaInfo

                ' Determine ItemId
                If Not (Request.Params("ItemId") Is Nothing) Then
                    itemId = Int32.Parse(Request.Params("ItemId"))
                Else
                    itemId = Null.NullInteger()
                End If

                ' Determine stateId
                If Not (Request.Params("StateId") Is Nothing) Then
                    stateId = Int32.Parse(Request.Params("StateId"))
                End If

                If Not Page.IsPostBack Then
                    cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" & Localization.GetString("DeleteItem") & "');")
                    If Not Null.IsNull(itemId) Then
                        objInfo = objController.GetExitCriteriaInfo(itemId)
                        If Not objInfo Is Nothing Then
                            'Load data
                            fldCriteria.Text = objInfo.Criteria.ToString()
                            fldDescription.Text = objInfo.Description.ToString()
                            stateId = objInfo.StateID
                            cmdDelete.Visible = True
                        Else ' security violation attempt to access item not related to this Module
                            Response.Redirect(NavigateURL(), True)
                        End If
                    End If
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click
            Try
                ' Only Update if the Entered Data is Valid
                If Page.IsValid = True Then
                    Dim objInfo As New ExitCriteriaInfo

                    Dim objController As New ExitCriteriaController
                    If Not Null.IsNull(itemId) Then
                        objInfo = objController.GetExitCriteriaInfo(itemId)
                    End If

                    objInfo.ModuleID = ModuleId
                    objInfo.CreatedByUser = UserInfo.UserID
                    objInfo.CreatedDate = Date.Now()
                    objInfo.Criteria = fldCriteria.Text.Trim()
                    objInfo.CreatedByUser = UserInfo.UserID
                    objInfo.CreatedDate = Date.Now()
                    objInfo.Criteria = fldCriteria.Text.Trim()
                    objInfo.Description = fldDescription.Text.Trim()

                    If Null.IsNull(itemId) Then
                        objInfo.StateID = stateId
                        objController.AddExitCriteria(objInfo)
                    Else
                        stateId = objInfo.StateID
                        objController.UpdateExitCriteria(objInfo)
                    End If

                    ' Redirect back to the portal home page
                    Response.Redirect(EditUrl("ItemId", stateId.ToString(), "EditStates"), True)
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
            Try
                Response.Redirect(EditUrl("ItemId", stateId.ToString(), "EditStates"), True)
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub cmdDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdDelete.Click
            Try
                If Not Null.IsNull(itemId) Then
                    Dim objController As New ExitCriteriaController
                    Dim objInfo As ExitCriteriaInfo = objController.GetExitCriteriaInfo(itemId)
                    stateId = objInfo.StateID
                    objController.DeleteExitCriteria(itemId)
                End If

                ' Redirect back to the portal home page
                Response.Redirect(EditUrl("ItemId", stateId.ToString(), "EditStates"), True)
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
#End Region

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

    End Class

End Namespace
